<template>
	<view>
		<!-- 地址 -->
		<navigator :url="addressUrl" class="address-section">
			<view class="order-content u-flex-row" v-if="currentAddress.contactor_name">
				<text class="iconfont icondizhi3 icon-address"></text>
				<view class="cen">
					<view class="top">
						<text class="name">{{currentAddress.contactor_name || ''}}</text>
						<text class="mobile">{{currentAddress.phone || ''}}</text>
					</view>
					<text class="address">{{currentAddress.address || ''}} {{currentAddress.street || ''}}</text>
				</view>
				<text class="iconfont iconyou1"></text>
			</view>
			<view class="none-address" v-else>
				点击添加地址
			</view>
			<image class="a-bg" src=""></image>
		</navigator>

		<view class="goods-section">
			<!-- 商品列表 -->
			<view class="g-item u-flex-row" v-for="(item, index) in products" :key="index">
				<image :src="item.product.image[0] && $tools.fixurl(item.product.image[0])"></image>
				<view class="right">
					<text class="title clamp">{{item.product.title}}</text>
					<text class="spec">{{item.sku.value}}</text>
					<view class="price-box u-flex-row">
						<text class="price">￥{{item.sku.price}}</text>
						<text class="number">x {{item.quantity}}</text>
					</view>
				</view>
			</view>
			
		</view>

		<!-- 优惠明细 -->
		<view class="yt-list">
			<view class="yt-list-cell b-b" v-if="couponList.length" @click="couponVisible = 1">
				<view class="cell-icon">
					券
				</view>
				<text class="cell-tit clamp">优惠券</text>
				<text class="cell-tip active">
					{{currentCoupon.name || '选择优惠券'}}
				</text>
				<text class="cell-more wanjia wanjia-gengduo-d"></text>
			</view>
		</view>
		
		<view class="yt-list" v-if="userinfo.score > 0 && totalPrice.score_price > 0">
			<view class="yt-list-cell b-b">
				<view class="cell-icon">积</view>
				<text class="cell-tit clamp">积分可抵扣￥{{totalPrice.score_price}}</text>
				<text class="cell-tip active" @tap="useScoreHandle(true)" v-if="!form.use_score">使用</text>	
				<text class="cell-tip active" v-else @tap="useScoreHandle(false)">积分 -{{appInfo && appInfo.config && (appInfo.config.shop.order.score_to_money * totalPrice.score_price).toFixed(0)}}</text>
			</view>
		</view>
		
		<view class="yt-list" v-if="userinfo.money > 0">
			<view class="yt-list-cell b-b">
				<view class="cell-icon">余</view>
				<text class="cell-tit clamp">余额可抵扣￥{{totalPrice.money_price}}</text>
				<text class="cell-tip active" @tap="useMoneyHandle(true)" v-if="!form.use_money">使用</text>
				<text class="cell-tip active" v-else @tap="useMoneyHandle(false)">余额 -￥{{totalPrice.money_price}}</text>
			</view>
		</view>
		
		<!-- 金额明细 -->
		<view class="yt-list">
			<view class="yt-list-cell b-b">
				<text class="cell-tit clamp">商品金额</text>
				<text class="cell-tip">￥ {{totalPrice.products_price || 0}}</text>
			</view>
			<view class="yt-list-cell b-b" v-if="totalPrice.discount_price">
				<text class="cell-tit clamp">优惠金额</text>
				<text class="cell-tip red">- ￥ {{totalPrice.discount_price || 0}}</text>
			</view>
			<view class="yt-list-cell b-b">
				<text class="cell-tit clamp">运费</text>
				<text class="cell-tip">￥ {{totalPrice.delivery_price || '免运费'}}</text>
			</view>
			<view class="yt-list-cell desc-cell">
				<text class="cell-tit clamp">备注</text>
				<input class="desc" type="text" v-model="form.remark" placeholder="请填写备注信息" placeholder-class="placeholder" />
			</view>
			<view class="yt-list-cell b-b total">
				<text class="cell-tit clamp">订单总额</text>
				<text class="cell-tip">￥ {{order_total_price}}</text>
			</view>
		</view>
		
		<!-- 底部 -->
		<view class="footer u-flex-row">
			<view class="price-content u-flex-row">
				<text>总金额</text>
				<text class="price-tip">￥</text>
				<text class="price">{{stopSale ? '-' : totalPrice.order_price}}</text>
			</view>
			<text class="submit" :class="{disabled: stopSale}" @click="submit">提交订单</text>
		</view>
		
		<coupon :list="couponList" v-model="couponVisible" @cancel="couponCancel" @change="couponChange"></coupon>
	
	</view>
</template>

<script>
	import { mapState, mapActions } from 'vuex'
	import coupon from './child/coupon'
	export default {
		components: {
			coupon
		},
		data() {
			return {
				products: [],
				checkAuth: true,
				desc: '', //备注
				payType: 1, //1微信 2支付宝
				couponList: [],
				addressData: {},
				currentAddress: {},
				form: {
					use_money: false,
					use_score: false
				},
				options: {},
				totalPrice: {},
				address_id: 0,
				stopSale: true,
				couponVisible: 0, //优惠券面板显示状态
				currentCoupon: {},
			}
		},
		async onLoad(options){
			options = await this.onLoadStart(options);
			this.options = options;
			await this.getUserinfo()
			if (options.sku_id) {
				this.form.product = {sku_id: parseInt(options.sku_id), quantity: parseFloat(options.quantity)}
				let data = await this.getProducts(options.sku_id, options.quantity);
				this.products = [data]
			} else {
				let products = [];
				let list = await this.getCartProducts();
				list.forEach(item => {
					if (item.is_selected) products.push(item);
				});
				this.products = products;
			}
			this.address_id = options.address_id || 0
			this.getUserAddress().then(res => {
				this.currentAddress = this.getDefaultAddress()
				this.address_id = this.currentAddress.id
				this.getTotalPrice()
			})
			if (this.$tools.has_addon('shopcoupon')) this.getUsableCoupons()
		},
		computed: {
			...mapState({
				cProducts: state => state.cart.products,
				tempProducts: state => state.cart.tempProducts,
				isTemp: state => state.cart.isTempProduct,
				address: state => state.user.address,
				appInfo: state => state.appInfo,
				userinfo: state => state.user.userinfo
			}),
			productsTotal() {
				let list = this.products
				let total = 0
				list.forEach((item, index) => {
					total += item.quantity * item.sku.price
				})
				return total.toFixed(2)
			},
			order_total_price() {
				// if (!this.totalPrice.order_price) return '-';
				let total = parseFloat(this.totalPrice.order_price)
				return total.toFixed(2);
			},
			addressUrl() {
				let form = Object.assign(this.options, {source: 1})
				return '/pages/address/index?' + this.$tools.queryStringify(form)
			}
		},
		methods: {
			...mapActions(['getCartProducts', 'getUserAddress', 'getUserinfo']),
			
			numberChange(data) {
				this.number = data.number;
			},
			changePayType(type){
				this.payType = type;
			},
			async getTotalPrice() {
				let form = Object.assign({address_id: this.address_id, desc: this.desc}, this.form)
				const query = `
					query($address_id: Int!, $product: ShopProductInfoInput, $use_money: Boolean, $use_score: Boolean) {
					  orderPrice(address_id: $address_id, product: $product, use_money: $use_money, use_score: $use_score) {
					    money_price
					    use_money_price
					    score_price
					    use_score_price
					    products_price
					    order_price
					    delivery_price
					    discount_price
					  }
					}
				`
				const result = await this.$gql.fetch(query, form);
				const err = result.getError('orderPrice');
				if (err) return result.show(err)
				const { orderPrice } = result.get();
				this.totalPrice = orderPrice;
				this.stopSale = false;
			},
			async getProducts(sku_id, quantity) {
				const query = `
					query ($id: Int!) {
						sku (id: $id) {
						    image
						    price
							value
						    product {
						      title
						      image
						    }
						  }
					}
				`
				const result = await this.$gql.fetch(query, {id: parseInt(sku_id)})
				const { sku } = result.get();
				// let data = await this.$http.post('shop/api/product/sku', {id: sku_id})
				let product = sku.product;
				delete sku.product;
				return {
					sku,
					product,
					quantity
				}
			},
			async submit(){
				if (this.stopSale) return
				let form = {
					address_id: this.currentAddress.id
				}
				
				if (this.currentCoupon) form.coupon_id = this.currentCoupon.id
				form = Object.assign(form, this.form)
				const query = `
					mutation($address_id: Int!, $product: ShopProductInfoInput, $use_money: Boolean, $use_score: Boolean, $remark: String) {
						createOrder(address_id: $address_id, product: $product, use_money: $use_money, use_score: $use_score, remark: $remark)
					}
				`
				const result = await this.$gql.fetch(query, form);
				const err = result.getError('createOrder');
				if (err) return result.show(err);
				const order_sn = result.get('createOrder');
				this.getCartProducts();
				this.getUserinfo();
				uni.redirectTo({
					url: '/pages/pay/index?order_sn=' + order_sn
				})
				
			},
			getDefaultAddress() {
				if (this.address.length == 0) return {}
				let index = 0
				if (this.address_id) {
					this.address.forEach((item, i) => {
						if (item.id == this.address_id) index = i
					})
				} else {
					this.address.forEach((item, i) => {
						if (item.is_default == 1) index = i
					})
				}
				return this.address[index]
			},
			stopPrevent(){},
			getUsableCoupons() {
				this.$http.post('shopcoupon/api/coupon/usableCoupon', this.form).then(data => {
					this.couponList = data
				})
			},
			couponChange(item, index) {
				this.currentCoupon = item
				this.form.coupon_id = item.id
				this.couponVisible = 0
				this.getTotalPrice()
			},
			couponCancel(item, index) {
				this.currentCoupon = {};
				this.form.coupon_id = 0;
				this.couponVisible = 0;
				this.getTotalPrice();
			},
			useScoreHandle(val) {
				this.form.use_score = val;
				this.getTotalPrice();
			},
			useMoneyHandle(val) {
				this.form.use_money = val;
				this.getTotalPrice();
			}
		}
	}
</script>

<style lang="scss">
	page {
		background: $page-color-base;
		padding-bottom: 100upx;
	}

	.address-section {
		padding: 30upx 0;
		background: #fff;
		position: relative;

		.order-content {
			display: flex;
			align-items: center;
		}

		.icon-address {
			flex-shrink: 0;
			display: flex;
			align-items: center;
			justify-content: center;
			width: 90upx;
			color: #888;
			font-size: 44upx;
		}

		.cen {
			display: flex;
			flex-direction: column;
			flex: 1;
			font-size: 28upx;
			color: $font-color-dark;
		}

		.name {
			font-size: 34upx;
			margin-right: 24upx;
		}

		.address {
			margin-top: 16upx;
			margin-right: 20upx;
			color: $font-color-light;
		}

		.iconyou1 {
			font-size: 32upx;
			color: $font-color-light;
			margin-right: 30upx;
		}

		.a-bg {
			position: absolute;
			left: 0;
			bottom: 0;
			display: block;
			width: 100%;
			height: 5upx;
		}
		
		.none-address {
			padding: $font-base + 5upx;
			font-size: $font-base + 5upx;
			color: darkgrey;
			display: flex;
			justify-content: center;
			
		}
	}

	.goods-section {
		margin-top: 16upx;
		background: #fff;
		padding-bottom: 1px;

		.g-header {
			display: flex;
			align-items: center;
			height: 84upx;
			padding: 0 30upx;
			position: relative;
		}

		.logo {
			display: block;
			width: 50upx;
			height: 50upx;
			border-radius: 100px;
		}

		.name {
			font-size: 30upx;
			color: $font-color-base;
			margin-left: 24upx;
		}

		.g-item {
			display: flex;
			margin: 20upx 30upx;

			image {
				flex-shrink: 0;
				display: block;
				width: 140upx;
				height: 140upx;
				border-radius: 4upx;
			}

			.right {
				flex: 1;
				padding-left: 24upx;
				overflow: hidden;
			}

			.title {
				font-size: 30upx;
				color: $font-color-dark;
			}

			.spec {
				font-size: 26upx;
				color: $font-color-light;
			}

			.price-box {
				display: flex;
				align-items: center;
				font-size: 32upx;
				color: $font-color-dark;
				padding-top: 10upx;

				.price {
					margin-bottom: 4upx;
				}
				.number{
					font-size: 26upx;
					color: $font-color-base;
					margin-left: 20upx;
				}
			}

			.step-box {
				position: relative;
			}
		}
	}
	.yt-list {
		margin-top: 16upx;
		background: #fff;
	}

	.yt-list-cell {
		display: flex;
		flex-direction: row;
		align-items: center;
		padding: 10upx 30upx 10upx 40upx;
		line-height: 70upx;
		position: relative;

		&.cell-hover {
			background: #fafafa;
		}

		&.b-b:after {
			left: 30upx;
		}

		.cell-icon {
			height: 32upx;
			width: 32upx;
			font-size: 22upx;
			color: #fff;
			text-align: center;
			line-height: 32upx;
			background: #f85e52;
			border-radius: 4upx;
			margin-right: 12upx;

			&.hb {
				background: #ffaa0e;
			}

			&.lpk {
				background: #3ab54a;
			}

		}

		.cell-more {
			align-self: center;
			font-size: 24upx;
			color: $font-color-light;
			margin-left: 8upx;
			margin-right: -10upx;
		}

		.cell-tit {
			flex: 1;
			font-size: 26upx;
			color: $font-color-light;
			margin-right: 10upx;
		}

		.cell-tip {
			font-size: 26upx;
			color: $font-color-dark;

			&.disabled {
				color: $font-color-light;
			}

			&.active {
				color: $base-color;
			}
			&.red{
				color: $base-color;
			}
		}

		&.desc-cell {
			.cell-tit {
				max-width: 90upx;
			}
		}

		.desc {
			flex: 1;
			font-size: $font-base;
			color: $font-color-dark;
		}
	}
	
	/* 支付列表 */
	.pay-list{
		padding-left: 40upx;
		margin-top: 16upx;
		background: #fff;
		.pay-item{
			display: flex;
			align-items: center;
			padding-right: 20upx;
			line-height: 1;
			height: 110upx;	
			position: relative;
		}
		.icon-weixinzhifu{
			width: 80upx;
			font-size: 40upx;
			color: #6BCC03;
		}
		.icon-alipay{
			width: 80upx;
			font-size: 40upx;
			color: #06B4FD;
		}
		.icon-xuanzhong2{
			display: flex;
			align-items: center;
			justify-content: center;
			width: 60upx;
			height: 60upx;
			font-size: 40upx;
			color: $base-color;
		}
		.tit{
			font-size: 32upx;
			color: $font-color-dark;
			flex: 1;
		}
	}
	
	.footer{
		position: fixed;
		left: 0;
		bottom: 0;
		z-index: 995;
		display: flex;
		align-items: center;
		width: 100%;
		height: 90upx;
		justify-content: space-between;
		font-size: 30upx;
		background-color: #fff;
		z-index: 998;
		color: $font-color-base;
		box-shadow: 0 -1px 5px rgba(0,0,0,.1);
		.price-content{
			padding-left: 30upx;
		}
		.price-tip{
			color: $base-color;
			margin-left: 8upx;
		}
		.price{
			font-size: 36upx;
			color: $base-color;
		}
		.submit{
			display:flex;
			align-items:center;
			justify-content: center;
			width: 320upx;
			height: 100%;
			color: #fff;
			font-size: 32upx;
			background-color: $base-color;
		}
		.disabled {
			background: #ddd;
		}
	}
	
	/* 优惠券面板 */
	.mask{
		display: flex;
		align-items: flex-end;
		position: fixed;
		left: 0;
		top: var(--window-top);
		bottom: 0;
		width: 100%;
		background: rgba(0,0,0,0);
		z-index: 9995;
		transition: .3s;
		
		.mask-content{
			width: 100%;
			max-height: 70vh;
			background: #f3f3f3;
			transform: translateY(100%);
			transition: .3s;
			overflow-y:scroll;
			height: auto;
		}
		&.none{
			display: none;
		}
		&.show{
			background: rgba(0,0,0,.4);
			
			.mask-content{
				transform: translateY(0);
			}
		}
	}

	/* 优惠券列表 */
	.coupon-item{
		display: flex;
		flex-direction: column;
		margin: 20upx 24upx;
		background: #fff;
		.con{
			display: flex;
			align-items: center;
			position: relative;
			height: 120upx;
			padding: 0 30upx;
			&:after{
				position: absolute;
				left: 0;
				bottom: 0;
				content: '';
				width: 100%;
				height: 0;
				border-bottom: 1px dashed #f3f3f3;
				transform: scaleY(50%);
			}
		}
		.left{
			display: flex;
			flex-direction: column;
			justify-content: center;
			flex: 1;
			overflow: hidden;
			height: 100upx;
		}
		.title{
			font-size: 32upx;
			color: $font-color-dark;
			margin-bottom: 10upx;
		}
		.time{
			font-size: 24upx;
			color: $font-color-light;
		}
		.right{
			display: flex;
			flex-direction: column;
			justify-content: center;
			align-items: center;
			font-size: 26upx;
			color: $font-color-base;
			height: 100upx;
		}
		.price{
			font-size: 44upx;
			color: $base-color;
			&:before{
				content: '￥';
				font-size: 34upx;
			}
		}
		.tips{
			font-size: 24upx;
			color: $font-color-light;
			line-height: 60upx;
			padding-left: 30upx;
		}
		.circle{
			position: absolute;
			left: -6upx;
			bottom: -10upx;
			z-index: 10;
			width: 20upx;
			height: 20upx;
			background: #f3f3f3;
			border-radius: 100px;
			&.r{
				left: auto;
				right: -6upx;
			}
		}
	}
	
	.total .cell-tit {
		font-size: 30rpx;
	}
	
	.total .cell-tip {
		font-size: 32rpx;
		color: #fa436a;
	}

</style>
